package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.Merchant;
import com.example.wechattakeaway.entity.UserAttention;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.mapper.AttentionMapper;
import com.example.wechattakeaway.mapper.MerchantMapper;
import com.example.wechattakeaway.service.AttentionService;
import com.example.wechattakeaway.utils.PageUtil;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.AttentionVo;
import com.example.wechattakeaway.vo.ResultVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Repository
@Slf4j
public class AttentionServiceImpl implements AttentionService {

    @Autowired
    AttentionMapper attentionMapper;

    @Autowired
    MerchantMapper merchantMapper;


    @Override
    public ResultVO InsertUserAttention(Integer userId, Integer merchantId) {
        try{
            attentionMapper.InsertUserAttention(userId,merchantId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO SelectAttentionByUser(Integer pageNum,Integer userId) {
        List<UserAttention> attentionList = new ArrayList<>();

        PageHelper.startPage(pageNum,5,true);
        attentionList = attentionMapper.SelectAttentionByUser(userId);
        System.out.println(attentionList.size());
        PageInfo pageInfo = new PageInfo(attentionList);
        //接下来是对返回的内容进行封装，因为列表里面只有id的值
        List<AttentionVo> attentionVoList = new ArrayList<>();
        for (UserAttention userAttention:attentionList) {
            AttentionVo attentionVo = new AttentionVo();
            attentionVo.setAttentionId(userAttention.getAttentionId());
            //依次遍历出所有id的商家信息，然后封装
            Merchant merchant = merchantMapper.selectByPrimaryKey(userAttention.getMerchantId());
            attentionVo.setMerchantName(merchant.getMerchantName());
            attentionVo.setMerchantAddress(merchant.getMerchantAddress());
            attentionVo.setDescription(merchant.getDescription());
            attentionVo.setMerchantPhone(merchant.getMerchantPhone());
            attentionVo.setImgUrl(merchant.getImgUrl());
            attentionVoList.add(attentionVo);
        }

        pageInfo.setList(attentionVoList);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(attentionVoList);

        return ResultVOUtil.success(hashMap);
    }

    @Override
    public ResultVO DeleteAttentionById( Integer merchantId) {
        try{
            attentionMapper.DeleteAttentionById( merchantId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }


}
